import '../../utils/lodash_fix'
let _ = require("../../utils/lodash.min");
import { getorder, getcommunity, getuserhouse } from '../../utils/api';
import { getStorageSyncTime, setStorageSyncSecond } from '../../utils/storage';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderlist: [],
    active: 0,
    nav: [
      { name: '全部' },
      { name: '未支付' },
      { name: '已支付' },
    ],
    setlist: []
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.qurey()
  },
  onChange(e) {
    let act = e.detail.index
    this.setData({ active: act })
    let list = this.data.setlist
    let newlist = []
    if (_.isEqual(act, 1)) {
      list.forEach(e => {
        if (e.state === '未支付') {
          newlist.push(e)
        }
      })
      this.setData({ orderlist: newlist })
    } else if (_.isEqual(act, 2)) {
      list.forEach(e => {
        if (e.state === '成功') {
          newlist.push(e)
        }
      })
      this.setData({ orderlist: newlist })
    } else if (_.isEqual(act, 0)) {
      this.getorder()
    }

  },
  qurey() {
    let a = getStorageSyncTime('community')
    if (a) {
      getcommunity().then(res => {
        if (res.statusCode == 200) {
          setStorageSyncSecond('community', res.data.data, 10)
          this.qurey()
        }
      })
    } else {
      let a = getStorageSyncTime('houselist')
      if (a) {
        getuserhouse().then(res => {
          if (_.isEqual(res.statusCode, 200)) {
            let arr = wx.getStorageSync('community')
            let list = res.data.data
            arr.forEach(e => {
              list.forEach((v) => {
                if (e.uuid == v.community_uuid) {
                  v.community_name = e.community_name
                }
              })
            })
            setStorageSyncSecond('houselist', list, 10)
          }
        })
      } else {
        this.getorder()
      }
    }
  },
  getorder(e) {
    getorder().then(res => {
      console.log(res)
      if (_.isEqual(res.statusCode, 200)) {
        let list = wx.getStorageSync('houselist')
        let arr = res.data.data
        arr.forEach((e) => {
          list.forEach((v) => {
            e.house_uuid = v.uuid
            e.houseitem = v
          })
        })
        this.setData({ orderlist: arr, setlist: arr })
      }
    })
  },
  todel(e) {
    let item = e.currentTarget.dataset.item
    wx.setStorageSync('orditem', item)
    wx.navigateTo({
      url: '/pages/orddel/orddel',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})